package cn.xabad.server.thea.ext

import java.security.MessageDigest


@Throws(Exception::class)
fun md5(inStr: String): String {
    var md5: MessageDigest? = null
    try {
        md5 = MessageDigest.getInstance("MD5")
    } catch (e: Exception) {
        println(e.toString())
        e.printStackTrace()
        return ""
    }

    val byteArray = inStr.toByteArray(charset("UTF-8"))
    val md5Bytes = md5!!.digest(byteArray)
    val hexValue = StringBuffer()
    for (i in md5Bytes.indices) {
        val `val` = md5Bytes[i].toInt() and 0xff
        if (`val` < 16) {
            hexValue.append("0")
        }
        hexValue.append(Integer.toHexString(`val`))
    }
    return hexValue.toString()
}